package com.newer.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newer.domain.Material;
import com.newer.domain.MaterialCategory;
import com.newer.domain.Supplier;
import com.newer.dto.MaterialDto;
import com.newer.dto.MaterialSupplierDto;
import com.newer.service.MaterialCategoryService;
import com.newer.service.MaterialService;
import com.newer.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("material")
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    @Autowired
    private MaterialCategoryService materialCategoryService;

    @Autowired
    private SupplierService supplierService;

    //获取产品类型跟供应商表信息
    @RequestMapping("materialSupplier")
    public MaterialSupplierDto materialSupplier(Model model){

        List<MaterialCategory> categories=this.materialCategoryService.list();

        List<Supplier> suppliers=this.supplierService.list();

        System.out.println(categories);
        System.out.println(suppliers);
        model.addAttribute("categories",categories);
        model.addAttribute("suppliers",suppliers);

        return new MaterialSupplierDto(categories,suppliers);
    }

    //查询
    @RequestMapping("selectMaterial")
    public Page selectMaterial(int page,int size,String name, Model model){

        Page page1=new Page();

        page1.setSize(size);
        page1.setCurrent(page);


        Page<Material> materials= this.materialService.selectMaterial(page1,name);

        System.out.println(materials.getSize());
        System.out.println(materials.getPages());
        System.out.println(materials.getCurrent());

        if(materials!=null){
            model.addAttribute("material",materials);
            return materials;
        }

        return materials;

    }

    //单表的查询
    @RequestMapping("Materials")
    public List<Material> Materials(){

        return this.materialService.list();
    }


    //添加
    @RequestMapping("insertMaterial")
    public Boolean insertMaterial(@RequestBody Material material){
        material.setMaterialid(null);

        System.out.println(material);
       return this.materialService.save(material);
    }

    //删除
    @RequestMapping("deleteMaterial")
    public Boolean deleteMaterial(int id){

        return this.materialService.removeById(id);
    }

    //修改
    @RequestMapping("updateMaterial")
    public Boolean updateMaterial(@RequestBody Material material){

       material.setMaterialCategory(null);
       material.setSupplier(null);

        return this.materialService.updateById(material);
    }

}
